package com.osfw.system.service.monitor.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.osfw.framework.utils.DateUtils;
import com.osfw.framework.utils.StringUtils;
import com.osfw.framework.utils.text.Convert;
import com.osfw.system.entity.monitor.SysLogininfor;
import com.osfw.system.entity.monitor.SysOperLog;
import com.osfw.system.mapper.monitor.SysOperLogMapper;
import com.osfw.system.service.monitor.ISysOperLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.List;

/**
 * 操作日志 服务层处理
 */
@Service
public class SysOperLogServiceImpl extends ServiceImpl<SysOperLogMapper, SysOperLog> implements ISysOperLogService {
    @Autowired
    private SysOperLogMapper operLogMapper;

    /**
     * 新增操作日志
     *
     * @param operLog 操作日志对象
     */
    @Override
    public void insertOperlog(SysOperLog operLog) {
        operLog.setOperTime(DateUtils.getNowDate());
        save(operLog);
    }

    /**
     * 查询系统操作日志集合
     *
     * @param operLog 操作日志对象
     * @return 操作日志集合
     */
    @Override
    public List<SysOperLog> selectOperLogList(SysOperLog operLog) {
        LambdaQueryWrapper<SysOperLog> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotEmpty(operLog.getOperIp()), SysOperLog::getOperIp, operLog.getOperIp());
        queryWrapper.like(StringUtils.isNotEmpty(operLog.getTitle()), SysOperLog::getTitle, operLog.getTitle());
        queryWrapper.eq(operLog.getBusinessType() != null, SysOperLog::getBusinessType, operLog.getBusinessType());
        queryWrapper.in(operLog.getBusinessTypes() != null && operLog.getBusinessTypes().length > 0, SysOperLog::getBusinessTypes, operLog.getBusinessTypes());
        queryWrapper.eq(operLog.getStatus() != null, SysOperLog::getStatus, operLog.getStatus());
        queryWrapper.like(StringUtils.isNotEmpty(operLog.getOperName()), SysOperLog::getOperName, operLog.getOperName());
        queryWrapper.ge(operLog.getBeginTime() != null, SysOperLog::getOperTime, operLog.getBeginTime());
        queryWrapper.le(operLog.getEndTime() != null, SysOperLog::getOperTime, operLog.getEndTime());
        return list(queryWrapper);
    }

    /**
     * 批量删除系统操作日志
     *
     * @param ids 需要删除的数据
     * @return
     */
    @Override
    public boolean deleteOperLogByIds(String ids) {
        return removeByIds(Arrays.asList(Convert.toStrArray(ids)));
    }

    /**
     * 查询操作日志详细
     *
     * @param operId 操作ID
     * @return 操作日志对象
     */
    @Override
    public SysOperLog selectOperLogById(Long operId) {
        return getById(operId);
    }

    /**
     * 清空操作日志
     */
    @Override
    public boolean cleanOperLog() {
        return remove(new LambdaQueryWrapper());
    }
}
